package com.ellecity06.debugtools.ui.fragment.request

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.cachedIn
import com.ellecity06.debugtools.base.BaseDebugViewModel
import com.ellecity06.debugtools.db.dao.RequestDao
import com.ellecity06.debugtools.ui.fragment.request.pagination.DebugRequestPagingSource

/**
 * @author ellecity06
 * @time 2023/4/19 17:22
 * @des
 */
internal class DebugRequestListViewModel(private val dao: RequestDao) : BaseDebugViewModel() {
    val data = Pager(PagingConfig(pageSize = 10, enablePlaceholders = false, initialLoadSize = 10)) {
        DebugRequestPagingSource(dao)
    }.flow.cachedIn(viewModelScope)
}

internal class DebugRequestViewModelFactory(
    private val dao: RequestDao
) : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        if (modelClass.isAssignableFrom(DebugRequestListViewModel::class.java)) {
            @Suppress("UNCHECKED_CAST")
            return DebugRequestListViewModel(dao) as T
        }
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}